<?php
class Cron extends CI_Controller
{
    public function commwapqop()
    {
        if (!is_cli()) die('request forbidden');
        set_time_limit(0);
        $this->load->database();
        $this->load->model('redis_model');
        try{
            while(true){
                $data=array();
                $res = $this->redis_model->lpop('qoplist');
                if ($res === false) break;
                $res = igbinary_unserialize($res);
                switch ($res['type']){
                    case 'rq':
                        $data['id']=$res['data']['qid'];
                        $data['score'] = '+1';
                        break;
                    case 'fq':
                        $data['id']=$res['data']['qid'];
                        $data['score'] = '+5';
                        break;
                    case 'cq':
                        $data['id']=$res['data']['qid'];
                        $data['score'] = '-5';
                        break;
                    case 'addans':
                        $data['id']=$res['data']['qid'];
                        $data['score'] = '+10';
                        break;
                    case 'cbeans':
                        $data['id']=$res['data']['qid'];
                        $data['score'] = '+'.round((float)$res['data']['num']*0.1);
                        break;
                }
                if (!empty($data)){
                    $this->db->reconnect();
                    $this->db->set('score','score'.$data['score'],false);
                    $this->db->where('id',$data['id']);
                    $this->db->update('comm_question');
                }
            }
        }catch (Exception $e){
            $err['type'] = 'crontab';
            $err['time'] = date('Y-m-d H:i:s',time());
            $err['data'] = 'qoplist error : '.$e->getMessage();
            $err = igbinary_serialize($err);
            $this->redis_model->rpush('errlist',$err);
            $this->db->close();
            exit();
        }

    }

    public function commwapansop()
    {
        if (!is_cli()) die('request forbidden');
        set_time_limit(0);
        $this->load->model('redis_model');
        $this->load->database();
        try{
            while(true){
                $data = array();
                $res = $this->redis_model->lpop('ansoplist');
                if ($res === false) break;
                $res = igbinary_unserialize($res);
                switch ($res['type']){
                    case 'agrans':
                        $data['id']=$res['data']['aid'];
                        $data['score'] = '+1';
                        break;
                    case 'cagrans':
                        $data['id']=$res['data']['aid'];
                        $data['score'] = '-1';
                        break;
                    case 'collans':
//                        $data['id']=$res['data']['aid'];
//                        $data['score'] = '+10';
                        break;
                    case 'ccollans':
//                        $data['id']=$res['data']['aid'];
//                        $data['score'] = '-10';
                        break;
                    case 'addcom':
//                        $data['id']=$res['data']['aid'];
//                        $data['score'] = '+10';
                        break;
                }
                if (!empty($data)){
                    $this->db->reconnect();
                    $this->db->set('score','score'.$data['score'],false);
                    $this->db->where('id',$data['id']);
                    $this->db->update('comm_answer');
                }
            }
        }catch (Exception $e){
            $err['type'] = 'crontab';
            $err['time'] = date('Y-m-d H:i:s',time());
            $err['data'] = 'ansoplist error : '.$e->getMessage();
            $err = igbinary_serialize($err);
            $this->redis_model->rpush('errlist',$err);
            $this->db->close();
            exit();
        }

    }
    public function commwapcomop()
    {
        if (!is_cli()) die('request forbidden');
        set_time_limit(0);
        $this->load->database();
        $this->load->model('redis_model');
        try{
            while(true){
                $data = array();
                $res = $this->redis_model->lpop('comoplist');
                if ($res === false) break;
                $res = igbinary_unserialize($res);
                switch ($res['type']){
                    case 'upcom':
                        $data['id']=$res['data']['cid'];
                        $data['score'] = '+1';
                        break;
                    case 'cupcom':
                        $data['id']=$res['data']['cid'];
                        $data['score'] = '-1';
                        break;
                    case 'rescom':
//                        $data['id']=$res['data']['cid'];
//                        $data['score'] = '+10';
                        break;
                }
                if (!empty($data)){
                    $this->db->reconnect();
                    $this->db->set('score','score'.$data['score'],false);
                    $this->db->where('id',$data['id']);
                    $this->db->update('comm_comments');
                }
            }
        }catch (Exception $e){
            $err['type'] = 'crontab';
            $err['time'] = date('Y-m-d H:i:s',time());
            $err['data'] = 'comoplist error : '.$e->getMessage();
            $err = igbinary_serialize($err);
            $this->redis_model->rpush('errlist',$err);
            $this->db->close();
            exit();
        }
    }

}